package com.mall.db.api;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.mapper.SmsCouponMapper;
import com.mall.pojo.SmsCoupon;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class SmsCouponApiImpl implements SmsCouponApi {

    @Autowired
    private SmsCouponMapper couponMapper;

    /**
     * 根据优惠券名称和类型分页获取优惠券列表
     *
     * @param pageNum
     * @param pageSize
     * @param name
     * @param type
     * @return
     */
    @Override
    public Page<SmsCoupon> list(Integer pageNum, Integer pageSize, String name, Integer type) {
        Page<SmsCoupon> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsCoupon> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(ObjectUtil.isNotNull(name), SmsCoupon::getName, name)
                .eq(ObjectUtil.isNotNull(type), SmsCoupon::getType, type);

        return couponMapper.selectPage(page,wrapper);
    }

    /**
     * 添加优惠券
     * @param coupon
     * @return
     */
    @Override
    public Long addIntoCoupon(SmsCoupon coupon) {
        couponMapper.insert(coupon);
        return coupon.getId();
    }

    /**
     * 删除优惠券
     * @param id
     * @return
     */
    @Override
    public Integer deleteById(Integer id) {
        return couponMapper.deleteById(id);
    }

    /**
     * 查询优惠券
     * @param id
     * @return
     */
    @Override
    public SmsCoupon findById(Integer id) {
        return couponMapper.selectById(id);
    }

    /**
     * 修改优惠券
     * @param coupon
     * @return
     */
    @Override
    public Integer updateIntoCoupon(SmsCoupon coupon) {
        return couponMapper.updateById(coupon);
    }
}
